<template>
	<view>
		<camera :device-position="cameraType" flash="off" class="camera" @error="error" v-if="showCamera"></camera>
		<image mode="widthFix" class="image" :src="photoPath" v-if="!showCamera"></image>
		<button class="camera_exchange" @tap="changeCameraType"></button>
		<view class="operate-container">
			<button type="primary" class="btn" @tap="clickBtn">{{ btnText }}</button>
			<button type="warn" class="btn" @tap="fresh">重拍</button>
		</view>
		<view class="notice-container">
			<text class="notice">注意事项</text>
			<text class="desc">
				<block v-if="type!='driverCardBack'">拍照时，必须把证件拍摄清晰并完整，否则影响识别结果。持证拍照的时候不许戴帽子、墨镜和面纱等遮挡五官的饰物，只拍摄上半身即可。手持证件，五官与证件必须同时拍摄到照片中。</block>
				<block v-if="type=='driverCardBack'">拍照时，必须把驾驶证背面拍摄的清晰完整，否则影响认证结果。</block>
			</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			type: null,
        	photoPath: '',
        	btnText: '拍照',
        	showCamera: true,
			cameraType: "back"
		};
	},
	onLoad:function(options){
		this.type = options.type;
	},
	methods: {
		changeCameraType() {
			this.cameraType = this.cameraType=="back"?"front":"back";
		},
		clickBtn() {
			let self = this;
			if(this.btnText == "拍照") {
				let ctx = uni.createCameraContext()
				ctx.takePhoto({
					quality:"high",
					success(res){
						self.photoPath = res.tempImagePath;
						self.showCamera = false;
						self.btnText = "提交"
					}
				})
			} else {
				//保存所有访问的页面
				let pages = getCurrentPages();
				//修改上一个页面的照片
				let prePage = pages[pages.length-2];
				prePage.$vm.updatePhoto(this.type, self.photoPath);
				uni.navigateBack({
					delta:1
				})
			}
		},
		fresh() {
			this.showCamera = true;
			this.btnText = "拍照";
		}
	}
};
</script>

<style lang="less">
@import url('identity_camera.less');
</style>
